package com.haiyou.dc.io;


import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class TestIO2 {

    private static String fileName = "E:\\IdeaProjects\\my-java-project\\dc-guava\\src\\main\\java\\com\\haiyou\\dc\\io\\io.txt";
    private static String descfileName = "E:\\IdeaProjects\\my-java-project\\dc-guava\\src\\main\\java\\com\\haiyou\\dc\\io\\io2.txt";

    public static void main(String[] args) throws IOException {

//        test2();
//        test3();
//        test4();
//        test5();
        test6();
    }

    public static void test2() throws IOException {
        try (InputStream in = new FileInputStream(fileName)) {
            int ch;
            while ((ch = in.read()) != -1) {
                System.out.print((char) ch);
            }
        }
    }

    public static void test3() throws IOException {
        try (InputStream in = new BufferedInputStream(new FileInputStream(fileName));
             OutputStream out = new BufferedOutputStream(new FileOutputStream(descfileName))) {
            int ch;
            byte[] buff = new byte[1024];
            while ((ch = in.read(buff)) != -1) {
                String data = new String(buff, 0, ch);
                System.out.print(data);
                out.write(buff);
            }
            out.flush();
        }
    }

    public static void test4() throws IOException {
        try (Reader in = new BufferedReader(new FileReader(fileName));
             Writer out = new BufferedWriter(new FileWriter(descfileName))) {
            int ch;
            char[] buff = new char[1024];
            while ((ch = in.read(buff)) != -1) {
                String data = new String(buff, 0, ch);
                System.out.print(data);
                out.write(buff);
            }
//            out.flush();
        }
    }

    public static void test5() throws IOException {
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
             BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(descfileName));) {
            String data;
            while ((data = bufferedReader.readLine()) != null) {
                System.out.println(data);
                bufferedWriter.write(data);
                bufferedWriter.newLine();
            }
            //刷新流
            bufferedWriter.flush();
        }
    }

    public static void test6() throws IOException {
        FileInputStream in = new FileInputStream(fileName);
        FileOutputStream out = new FileOutputStream(descfileName);
        FileChannel inChannel = in.getChannel();
        FileChannel outChannel = out.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(4 * 1024);
        long startTime = System.currentTimeMillis();
        while (inChannel.read(buffer) != -1) {
            buffer.flip();
            outChannel.write(buffer);
            buffer.clear();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("总共耗时" + (endTime - startTime) + "ms");
    }

}
